package ua.pp.bizon.yandex.uploader.atom;

import ua.pp.bizon.yandex.uploader.remote.RemoteAlbumFactory;
import ua.pp.bizon.yandex.uploader.remote.RemotePhotoCreator;

import java.io.File;
import java.util.LinkedList;

import org.apache.abdera.model.Element;
import org.apache.http.HttpException;

public class EntryAlbum extends AlbumsList {

	public static final String PARENT = "album";
	public static final String PHOTOS = "photos";
	public static final String SELF = "self";
	private PhotosList list;

	private AppLink photosLink;

	/**
	 *   
	 */
	public EntryAlbum(Element e) {
		super(e, "", new LinkedList<EntryAlbum>());
	}

	/**
	 * creates photo remotely
	 * 
	 * @param from
	 *            - path to local photo that should be uploaded
	 * @return new photo
	 * @throws HttpException
	 *             if some exception occurs
	 */
	public Photo createPhoto(File from) throws HttpException {
		Photo p = new RemotePhotoCreator().upload(from, this);
		getList().addPhoto(p);
		return p;
	}

	public PhotosList getList() {
		if (list == null) {
			list = createPhotosList();
		}
		return list;
	}

	/**
	 * @return name of album
	 */
	public String getName() {
		return getFirst("title").getText();
	}

	public AppLink getPhotosLink() {
		if (photosLink == null) {
			photosLink = getLink(PHOTOS);
		}
		return photosLink;
	}

	@Override
	public String toString() {
		return "EntryAlbum [getID()=" + getID() + ", getName()=" + getName() + ", albums=" + getAlbums() + "]";
	}

	protected PhotosList createPhotosList() {
		return PhotosListFactory.create(this.getPhotosLink());
	}

	protected EntryAlbum remoteCreate(String foldername) throws HttpException {
		EntryAlbum album = new RemoteAlbumFactory().createAlbum(foldername, this);
		getAlbums().add(album);
		return album;
	}

}
